<?php


class portal extends Controller {

	protected $user;

	function __construct() {
		parent::__construct();
		$this->session();
		$this->user = new user();
	}
	
	private function secure() {
		if( $_SESSION['portaladmin'] !== true ) {
			header('Location: /portal');
		}
	}

	function index() {
		$user = '';
		$pass = '';
		if( $_SERVER['REQUEST_METHOD'] == 'POST') {
			if( $_POST['username'] == 'admin' 
			&& $_POST['password'] == 'admin' ) {
				$_SESSION['portaladmin'] = true;
				header('Location: /portal/users'); 
			}
		}
		new View('portal/template', array(
			'content' => BufferedView::instance('portal/login', array(
				'user' => $user,
				'pass' => $pass,
				'action' => '/portal'
			) ),
		) );
	}
	
	function login() {
	
	}
	
	function users() {
		$users = $this->user->getAllUsers();
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'users') ),
			'content' => ( $param === 'invalid' ? 'Please fill in all fields' : '' )
			. BufferedView::instance('portal/users', array(
				'users' => $users
			) )->getBuffer()
			. BufferedView::instance('portal/formStudent', array(
				'action' => '/portal/addstudent'
			) )->getBuffer()
		) );
	}
	
	/*ADDED PROFESSORS LIST*/
	function professors() {
		$users = $this->user->getAllProfessors();
		new View('template', array( 
			'heading' => 'Manage Professors',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'professors') ),
			'content' => BufferedView::instance('portal/professors', array(
				'users' => $users
			) )
			.( $param === 'invalid' ? 'Please fill in all fields' : '' )
			. BufferedView::instance('portal/form', array(
				'action' => '/portal/add'
			) )
			
		) );
	}
	
	function add($param = NULL) {
	
		if (!empty($_POST['stud_no']) && !empty($_POST['password']) && 
			!empty($_POST['lastname']) && !empty($_POST['firstname']) &&
			!empty($_POST['middlename']) &&
			!empty($_POST['email']) ) {
			
			$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
			$studentNumberSize = "/^.{9}$/";
			$password = "/^.{6,10}$/";
			
			if( ctype_digit( $_POST['stud_no'] ) && preg_match( $studentNumberSize,$_POST['stud_no'] ) 
				&& preg_match( $pattern,$_POST['email'] ) && preg_match( $password,$_POST['password'] )  )
			{
				$this->user->insertUserComplete(
				$_POST['stud_no'], 
				$_POST['password'], 
				$_POST['lastname'], 
				$_POST['firstname'], 
				$_POST['middlename'], 
				$_POST['admin'], 
				$_POST['email']
				);
				
					header('Location: /portal/professors');
					new View('template', array( 
						'heading' => 'Manage Professors',
						'globalnav' => new BufferedView('org/globalnav'),
						'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
						'content' => BufferedView::instance('portal/form', array(
							'users' => $users
						) )
					) );
			}
			else{
				$users = $this->user->getAllProfessors();
				
				if( !(ctype_digit( $_POST['stud_no'] )) || !(preg_match( $studentNumberSize,$_POST['stud_no'])) )
				{
					$param = 'invalid format';
				}
				if( !(preg_match( $pattern,$_POST['email'])))
				{
					$param2 = 'invalid email';
				}
				if( !(preg_match( $password,$_POST['password'])))
				{
					$param3 = 'short password';
				}
			
				new View('template', array( 
				'heading' => 'Add Professors',
				'globalnav' => new BufferedView('org/globalnav'),
				'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'professors') ),
				'content' =>BufferedView::instance('portal/professors', array(
					'users' => $users
				) )->getBuffer()
				. ($param === 'invalid format' ? '<font color=red>Invalid student number </font><br/>': '' )
				. ($param3 === 'short password' ? '<font color=red>Password should be 6-10 characters long</font><br/>': '' )
				. ($param2 === 'invalid email' ? '<font color=red>Invalid email format </font>': '' )
				. BufferedView::instance('portal/form', array(
					'action' => '/portal/add'
				) )->getBuffer()
				));
			}
		}
		else {
			$param = 'invalid';
			$users = $this->user->getAllProfessors();
			
			new View('template', array( 
			'heading' => 'Add Professors',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'professors') ),
				'content' => BufferedView::instance('portal/professors', array(
					'users' => $users
				) )->getBuffer()
				.( $param === 'invalid' ? 'Please fill in all fields' : '' )
				. BufferedView::instance('portal/form', array(
					'action' => '/portal/add'
				) )->getBuffer()
			));
		}
	}
	
	function addstudent($param = NULL) {
	
		if (!empty($_POST['stud_no']) && !empty($_POST['password']) && 
			!empty($_POST['lastname']) && !empty($_POST['firstname']) &&
			!empty($_POST['middlename']) &&
			!empty($_POST['email']) ) {
			
			$pattern = "/^[a-zA-Z0-9._-]+@[a-zA-Z0-9-]+\.[a-zA-Z.]{2,5}$/";
			$studentNumberSize = "/^.{9}$/";
			$password = "/^.{6,10}$/";
			
			if( ctype_digit( $_POST['stud_no'] ) && preg_match( $studentNumberSize,$_POST['stud_no'] ) 
				&& preg_match( $pattern,$_POST['email'] ) && preg_match( $password,$_POST['password'] )  )
			{
				$this->user->insertUserComplete(
				$_POST['stud_no'], 
				$_POST['password'], 
				$_POST['lastname'], 
				$_POST['firstname'], 
				$_POST['middlename'], 
				$_POST['admin'], 
				$_POST['email']
				);
					header('Location: /portal/users');
					new View('template', array( 
						'heading' => 'Manage Users',
						'globalnav' => new BufferedView('org/globalnav'),
						'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
						'content' => BufferedView::instance('portal/formStudent', array(
							'users' => $users
						) )
					) );
			}
			else {
				if( !(ctype_digit( $_POST['stud_no'] )) || !(preg_match( $studentNumberSize,$_POST['stud_no'])) )
				{
					$param = 'invalid format';
				}
				if( !(preg_match( $pattern,$_POST['email'])))
				{
					$param2 = 'invalid email';
				}
				if( !(preg_match( $password,$_POST['password'])))
				{
					$param3 = 'short password';
				}
				
				$users = $this->user->getAllUsers();
			
				new View('template', array( 
						'heading' => 'Add User',
						'globalnav' => new BufferedView('org/globalnav'),
						'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
						'content' => BufferedView::instance('portal/users', array(
							'users' => $users
						) )->getBuffer()
						. ($param === 'invalid format' ? '<font color=red>Invalid student number </font><br/>': '' )
						. ($param3 === 'short password' ? '<font color=red>Password should be 6-10 characters long </font><br/>': '' )
						. ($param2 === 'invalid email' ? '<font color=red>Invalid email format </font>': '' )
						. BufferedView::instance('portal/formStudent', array(
							'action' => '/portal/addstudent'
						) )->getBuffer()
					) );
			}
		}
		else {
			$param = 'invalid';
			$users = $this->user->getAllUsers();
			
			new View('template', array( 
					'heading' => 'Add User',
					'globalnav' => new BufferedView('org/globalnav'),
					'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
					'content' => ( $param === 'invalid' ? 'Please fill in all fields' : '' )
					. BufferedView::instance('portal/users', array(
						'users' => $users
					) )->getBuffer()
					. BufferedView::instance('portal/formStudent', array(
						'action' => '/portal/addstudent'
					) )->getBuffer()
				) );
			
		}
	}
	
	function remove($param = NULL) {
		$this->user->deleteUser($param);
		header('Location: /portal/users');
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/formStudent', array(
				'users' => $users
			) )
		) );
	}
	
	function removeprof($param = NULL) {
		$this->user->deleteUser($param);
		header('Location: /portal/professors');
		new View('template', array( 
			'heading' => 'Manage Professors',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/form', array(
				'users' => $users
			) )
		) );
	}
	
	function update($param = NULL) {
		$row = $this->user->findUser($param);
		$row = $row[0];
		new View('template', array( 
			'heading' => 'Manage Users',
			'globalnav' => new BufferedView('org/globalnav'),
			'usernav' => '<font color=white><h3><center>MENU</center></h3></font>'.new BufferedView('siteadmin/nav', array('highlight'=>'manage') ),
			'content' => BufferedView::instance('portal/formEdit', array(
				'action' => '/portal/update_confirm',
				'studno' => $row['stud_no'],
				'password' => $row['password'],
				'lastname' => $row['lastname'],
				'firstname' => $row['firstname'],
				'middlename' => $row['middlename'],
				'admin' => $row['admin'],
				'email' => $row['email']
			) )
		) );
	}
	
	function update_confirm() {
		$this->user->updateUserComplete(
			$_POST['stud_no'], 
			$_POST['lastname'], 
			$_POST['firstname'], 
			$_POST['middlename'], 
			$_POST['password'], 
			$_POST['admin'], 
			$_POST['email']
		);
		
		
		if($_POST['admin']==0){
			header('Location: /portal/users');
		}
		else
		header('Location: /portal/professors'); 
		
	}

}



























	
